package com.gai.shop.entity;

import com.gai.shop.service.ProductService;
import com.gai.shop.service.impl.ProductServiceImpl;

import java.io.Serializable;

public class Page implements Serializable {
    /**
     * 当前页面
     */
    private Integer currentPage;
    /**
     * 总页面
     */
    private Integer totalPage;
    /**
     * 展示数量
     */
    private Integer showNumber;
    /**
     * 总数据
     */
    private Integer totalData;
    /**
     * 展示数据的第一条数据下标
     */
    private Integer showNumberFirstIndex;



    public Page() {

    }

    /**
     *
     * @param currentPage 当前页面
     * @param showNumber 展示数量
     * @param tid 商品类型id
     */
    public Page(Integer currentPage, Integer showNumber,Integer tid) {
        //总数据
        setTotalData(tid);
        //展示数量
        setShowNumber(showNumber);
        //总页面
        setTotalPage();
        //当前页面
        setCurrentPage(currentPage);
        //展示数据的第一条数据下标
        setShowNumberFirstIndex();
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    /**
     * 当前页面
     * @param currentPage
     */
    private void setCurrentPage(Integer currentPage) {
        if (currentPage < 1){
            this.currentPage = 1;
        }else if (currentPage > this.totalPage){
            this.currentPage = this.totalPage;
        }else {
            this.currentPage = currentPage;
        }
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    /**
     * 总页面
     */
    private void setTotalPage() {
        this.totalPage = this.totalData % this.showNumber == 0?this.totalData / this.showNumber:(this.totalData / this.showNumber) + 1;
    }

    public Integer getShowNumber() {
        return showNumber;
    }

    /**
     * 展示数量
     * @param showNumber
     */
    private void setShowNumber(Integer showNumber) {
        if (showNumber == null || showNumber < 1){
            this.showNumber = 3;
        }else {
            this.showNumber = showNumber;
        }
    }

    /**
     * 总数据
     */
    private void setTotalData(Integer tid) {
        ProductService pd = new ProductServiceImpl();
        Integer totalData = pd.selectTotalData(tid);
        if (totalData == null){
            this.totalData = 0;
        }else {
            this.totalData = totalData;
        }
    }

    public Integer getShowNumberFirstIndex() {
        return showNumberFirstIndex;
    }

    /**
     * 展示数据的第一条数据下标
     */
    private void setShowNumberFirstIndex() {
        this.showNumberFirstIndex = (this.currentPage - 1) * this.showNumber;
    }
}
